package com.omronservice.app.curve.controller;

import com.omronservice.app.curve.bean.copycurve.CopyCurve2Bean;
import com.omronservice.app.curve.bean.copycurve.CopyCurveBean;
import com.omronservice.app.curve.bean.maincurve.*;
import com.omronservice.app.curve.bean.sfcurve.SFCurveBean;
import com.omronservice.app.curve.service.*;
import com.omronservice.utils.ResultMsg;
import com.omronservice.utils.ResultStatusCode;
import com.omronservice.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Authorization;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * 曲线数据获取
 */
@Api(value = "曲线数据获取",description="所有曲线分析的接口都在这里")
@RestController
@RequestMapping("/CurveController")


public class CurveController {

    private static Logger logger = Logger.getLogger(CurveController.class);

    public static String status = "0";

    @Autowired
    CurveService curveService;

    @Autowired
    CopyCurveService copyCurveService;

    @Autowired
    SFCurveService sfCurveService;

    @Autowired
    DaysCurveService daysCurveService;

    @Autowired
    ExceptionCurveService exceptionCurveService;


    /**
     * 获取所有产线
     *
     */
    @ApiOperation(value = "获取所有产线", notes = "获取所有产线", response = HashMap.class)
    @Authorization("CooL")
    @GetMapping("/selectAllLine")
    public Object selectAllLine() {
        ResultMsg resultMsg;
        Date startdate = new Date();
        resultMsg = new ResultMsg(ResultStatusCode.OK.getResultCode(), ResultStatusCode.OK.getResultMessage(), curveService.selectAllLine());
        logger.info("所有产线执行SQL时间："+(((new Date()).getTime()-startdate.getTime())/1000));
        return resultMsg;
    }

    /**
     * 获取所有班次
     *
     */
    @ApiOperation(value = "获取所有班次", notes = "获取所有班次", response = HashMap.class)
    @Authorization("CooL")
    @GetMapping("/selectCLASS")
    public Object selectCLASS() {
        ResultMsg resultMsg;
        Date startdate = new Date();
        resultMsg = new ResultMsg(ResultStatusCode.OK.getResultCode(), ResultStatusCode.OK.getResultMessage(), curveService.selectCLASS());
        logger.info("所有产线执行SQL时间："+(((new Date()).getTime()-startdate.getTime())/1000));
        return resultMsg;
    }

    /**
     * 根据产线ID获取所有设备
     *
     */
    @ApiOperation(value = "根据产线ID获取所有设备", notes = "根据产线ID获取所有设备", response = HashMap.class)
    @Authorization("CooL")
    @GetMapping("/selectEqByLineId")
    public Object selectEqByLineId(@ApiParam(value = "产线 测试数据:1" ,required=true )@RequestParam(value = "line", required = true) String line) {
        ResultMsg resultMsg;
        Date startdate = new Date();
        resultMsg = new ResultMsg(ResultStatusCode.OK.getResultCode(), ResultStatusCode.OK.getResultMessage(), curveService.selectEqByLineId(line));
        logger.info("根据产线ID获取所有设备执行SQL时间："+(((new Date()).getTime()-startdate.getTime())/1000));
        return resultMsg;
    }

    /**
     *根据设备ID获取部件号
     *
     */
    @ApiOperation(value = "根据设备ID获取部件号", notes = "根据设备ID获取部件号", response = HashMap.class)
    @Authorization("CooL")
    @GetMapping("/selectPartNoById")
    public Object selectPartNoById(@ApiParam(value = "产线 测试数据:A" ,required=true )@RequestParam(value = "line", required = true) String line,
                                   @ApiParam(value = "设备ID 测试数据:A1 A2" ,required=true )@RequestParam(value = "eqIds", required = true) String[] eqId) {
        ResultMsg resultMsg;
        Date startdate = new Date();
        resultMsg = new ResultMsg(ResultStatusCode.OK.getResultCode(), ResultStatusCode.OK.getResultMessage(), curveService.selectPartNoById(line,eqId));
        logger.info("根据设备ID获取部件号时间："+(((new Date()).getTime()-startdate.getTime())/1000));
        return resultMsg;
    }


    /**
     * 主页产量图功能
     *
     */
    @ApiOperation(value = "主页产量图接口", notes = "主页产量图接口", response = MainCurveBean.class)
    @Authorization("CooL")
    @GetMapping("/mainCurve")
    public Object mainCurve(@ApiParam(value = "产线 测试数据:1" ,required=true )@RequestParam(value = "line", required = true) String line,
                            @ApiParam(value = "查询开始日期 测试数据:20170709" ,required=true )@RequestParam(value = "startDate", required = true) String startDate,
                            @ApiParam(value = "查询结束日期 测试数据:20170731" ,required=true )@RequestParam(value = "endDate", required = true) String endDate) {
        ResultMsg resultMsg;
        List<SelectEquBean> list = curveService.selectEqu(line);
        resultMsg = null;
        if(list!=null&&list.size()>0){
            SelectAllDataBean selectAllDataBean=new SelectAllDataBean();
            if(StringUtils.isNotEmpty(startDate)){
                selectAllDataBean.setStartDate(startDate);
            }
            if(StringUtils.isNotEmpty(endDate)){
                selectAllDataBean.setEndDate(endDate);
            }
            selectAllDataBean.setList(list);
            MainCurveBean mainCurveBean=new MainCurveBean();

           /* List<SelectCapacityBean> capacityList = curveService.selectCapacity(line);
            for(SelectCapacityBean bean:capacityList){

                switch (bean.getType()){
                    case "0":
                        mainCurveBean.setTotalCapacity(bean.getCapacity());
                        break;
                    case "A1":
                        mainCurveBean.setLdhsCapacity(bean.getCapacity());
                        break;
                    case "A21":
                        mainCurveBean.setHamlCapacity(bean.getCapacity());
                        break;
                    default:
                            break;

                }
            }*/
            String totalCapacity=curveService.selectCapacityString(line,"1",startDate,endDate);
            mainCurveBean.setTotalCapacity(totalCapacity);
            //String ldhsCapacity=curveService.selectCapacityString(line,"1",startDate);
            //mainCurveBean.setLdhsCapacity(totalCapacity);
            String hamlCapacity=curveService.selectCapacityString(line,"21",startDate,endDate);
            mainCurveBean.setHamlCapacity(hamlCapacity);
            selectAllDataBean.setLineId(line);
            Date startdate = new Date();
            List<MainChildBean> resultList=curveService.selectAllData2(selectAllDataBean);
            logger.info("主页面报表SQL时间："+(((new Date()).getTime()-startdate.getTime())/1000));
//            List<MainChildBean> childList=new ArrayList<>();
//            HashMap<String,MainChildBean> map=new HashMap<>();
//            for(SelectAllDataResBean bean:resultList){
//                if(map.containsKey(bean.getEquipmqntId())){
//                    switch (bean.getType()){
//                        case "0":
//                            map.get(bean.getEquipmqntId()).setOutPutNum(bean.getNum());
//                            break;
//                        case "1":
//                            map.get(bean.getEquipmqntId()).setPiecesNum(bean.getNum());
//                            break;
//                        case "2":
//                            map.get(bean.getEquipmqntId()).setQualityNum(bean.getNum());
//                            break;
//                            default:
//                                break;
//                    }
//                }else {
//                    MainChildBean mainChildBean=new MainChildBean();
//                    switch (bean.getType()){
//                        case "0":
//                            mainChildBean.setOutPutNum(bean.getNum());
//                            break;
//                        case "1":
//                            mainChildBean.setPiecesNum(bean.getNum());
//                            break;
//                        case "2":
//                            mainChildBean.setQualityNum(bean.getNum());
//                            break;
//                        default:
//                            break;
//                    }
//                    mainChildBean.setEquipmqntId(bean.getEquipmqntId());
//                    mainChildBean.setEquipmqntName(bean.getEquipmqntName());
//                    map.put(bean.getEquipmqntId(),mainChildBean);
//                }
//            }
//            for(String key:map.keySet()){
//                childList.add(map.get(key));
//            }

            mainCurveBean.setList(resultList);

            if(resultList != null) { //判断resultList是不是为空
                Integer maxOutPutNum = resultList.size() > 0 ? Integer.parseInt( resultList.get(0).getOutPutNum()) : 0;

                for(int i = 1; i < resultList.size(); i++) { //列表循环，找出产量最大值
                    if(Integer.parseInt(resultList.get(i).getOutPutNum()) > maxOutPutNum) {
                        maxOutPutNum = Integer.parseInt(resultList.get(i).getOutPutNum());
                    }
                }
                mainCurveBean.setMaxLimit(maxOutPutNum + "");
            }

            resultMsg = new ResultMsg(ResultStatusCode.OK.getResultCode(), ResultStatusCode.OK.getResultMessage(), mainCurveBean);
        }


        return resultMsg;
    }

    /**
     * 异常分析1
     *
     */
    @ApiOperation(value = "异常分析1接口", notes = "异常分析1接口", response = CopyCurveBean.class)
    @Authorization("CooL")
    @GetMapping("/exception1Curve")
    public Object exception1Curve(@ApiParam(value = "产线 测试数据:1" ,required=true )@RequestParam(value = "line", required = true) String line,
                             @ApiParam(value = "查询开始日期1 测试数据:20170709" ,required=true )@RequestParam(value = "startDate", required = true) String startDate,
                             @ApiParam(value = "查询结束日期1 测试数据:20170730" ,required=true )@RequestParam(value = "endDate", required = true) String endDate,
                             @ApiParam(value = "部件号ID，数组格式 测试数据 1,2,4" ,required=true )@RequestParam(value = "parts", required = true) String[] parts,
                                  @ApiParam(value = "设备ID 测试数据:A1 A2" ,required=true )@RequestParam(value = "eqIds", required = true) String[] eqId) {
        ResultMsg resultMsg;
        List<HashMap<String,String>> portList=curveService.selectPartNoALLById(line,eqId.length==0?null:eqId);
        List<String> partsNew=new ArrayList<>();
        List<String> eqIdsNew=new ArrayList<>();

        if(eqId!=null &&eqId.length>0){
            for(String id:eqId){
                eqIdsNew.add(id);
            }
        }

        for(String partNo:parts){
            for(HashMap<String,String> map:portList){
                if(partNo.equals(map.get("partNo"))&&eqIdsNew.contains(map.get("EQUIPMENT_ID"))){
                    partsNew.add(String.valueOf(map.get("uId")));
                }
            }
        }
        if(parts==null||parts.length==0){
            for(HashMap<String,String> map:portList){
                    partsNew.add(String.valueOf(map.get("uId")));
            }
        }
        int i = 0;
        String[] portsReturn=new String[partsNew.size()];
        for(String id:partsNew){
            portsReturn[i++]=id;
        }

        Date startdate = new Date();
        resultMsg = new ResultMsg(ResultStatusCode.OK.getResultCode(), ResultStatusCode.OK.getResultMessage(), exceptionCurveService.selectDays1(line,portsReturn,startDate,endDate));
        logger.info("异常分析1时间："+(((new Date()).getTime()-startdate.getTime())/1000));
        return resultMsg;
    }

    /**
     * 异常分析2
     *
     */
    @ApiOperation(value = "异常分析2接口", notes = "异常分析1接口", response = CopyCurveBean.class)
    @Authorization("CooL")
    @GetMapping("/exception2Curve")
    public Object exception2Curve(@ApiParam(value = "产线 测试数据:1" ,required=true )@RequestParam(value = "line", required = true) String line,
                                 @ApiParam(value = "查询开始日期1 测试数据:20170709" ,required=true )@RequestParam(value = "startDate", required = true) String startDate,
                                 @ApiParam(value = "查询结束日期1 测试数据:2017-07-30 10:10:10" ,required=true )@RequestParam(value = "endDate", required = true) String endDate,
                                 @ApiParam(value = "设备ID，数组格式" ,required=true )@RequestParam(value = "devices", required = true) String[] devices) {
        ResultMsg resultMsg;

        Date startdate = new Date();
        resultMsg = new ResultMsg(ResultStatusCode.OK.getResultCode(), ResultStatusCode.OK.getResultMessage(), exceptionCurveService.selectDays2(line,devices,startDate,endDate));
        logger.info("异常分析2时间："+(((new Date()).getTime()-startdate.getTime())/1000));
        return resultMsg;
    }


    /**
     * 印刷机分析1
     *
     */
    @ApiOperation(value = "印刷机分析1接口", notes = "印刷机分析1接口", response = CopyCurveBean.class)
    @Authorization("CooL")
    @GetMapping("/copy1Curve")
    public Object copy1Curve(@ApiParam(value = "产线 测试数据:1" ,required=true )@RequestParam(value = "line", required = true) String line,
                            @ApiParam(value = "查询开始日期1 测试数据:20170709" ,required=true )@RequestParam(value = "startDate1", required = true) String startDate1,
                            @ApiParam(value = "查询结束日期1 测试数据:20170730" ,required=true )@RequestParam(value = "endDate1", required = true) String endDate1,
                            @ApiParam(value = "查询开始日期2 测试数据:20170709 如果没选AB模式 传跟开始日期1一样的值过来" ,required=true )@RequestParam(value = "startDate2", required = true) String startDate2,
                            @ApiParam(value = "查询结束日期2 测试数据:20170730 如果没选AB模式 传跟结束日期1一样的值过来" ,required=true )@RequestParam(value = "endDate2", required = true) String endDate2,
                            @ApiParam(value = "项目选择 1栅线宽度 2湿重 3短路电流 4效率 5填充" ,required=true )@RequestParam(value = "type", required = true) String type,
                             @ApiParam(value = " 1 2 3 4" ,required=true )@RequestParam(value = "eqNo", required = true) String eqNo,
                             @ApiParam(value = "查询模式 1按照片 2按照小时 3按照秒" ,required=true )@RequestParam(value = "selectType", required = true) String selectType,
                             @ApiParam(value = " AB选择 0 没选择 1 选择" ,required=true )@RequestParam(value = "abFlg", required = true) String abFlg) {
        ResultMsg resultMsg;

        Date startdate = new Date();
        resultMsg = new ResultMsg(ResultStatusCode.OK.getResultCode(), ResultStatusCode.OK.getResultMessage(), copyCurveService.selectWidth(startDate1,endDate1,startDate2,endDate2,type,eqNo,line,abFlg,selectType));
        logger.info("印刷机分析1时间："+(((new Date()).getTime()-startdate.getTime())/1000));
        return resultMsg;
    }

    /**
     * 印刷机分析2
     *
     */
    @ApiOperation(value = "印刷机分析2接口", notes = "印刷机分析2接口", response = CopyCurve2Bean.class)
    @Authorization("CooL")
    @GetMapping("/copy2Curve")
    public Object copy2Curve(@ApiParam(value = "产线 测试数据:1" ,required=true )@RequestParam(value = "line", required = true) String line,
                            @ApiParam(value = "查询开始日期 测试数据:20170709" ,required=true )@RequestParam(value = "startDate", required = true) String startDate,
                            @ApiParam(value = "查询结束日期 测试数据:20170730" ,required=true )@RequestParam(value = "endDate", required = true) String endDate,
                            @ApiParam(value = "项目选择 1线宽 2湿重 3效率" ,required=true )@RequestParam(value = "type", required = true) String type,
                             @ApiParam(value = "查询模式 1按照片 2按照小时 3按照秒" ,required=true )@RequestParam(value = "selectType", required = true) String selectType,
                             @ApiParam(value = "印刷机" ,required=true )@RequestParam(value = "printType", required = true) String printType) {
        ResultMsg resultMsg;
        Date startdate = new Date();
        resultMsg = new ResultMsg(ResultStatusCode.OK.getResultCode(), ResultStatusCode.OK.getResultMessage(), copyCurveService.selectCurve2(line,startDate,endDate,type,printType,selectType));
        logger.info("印刷机分析2时间："+(((new Date()).getTime()-startdate.getTime())/1000));
        return resultMsg;
    }


    /**
     * 干燥炉分析1
     *
     */
    @ApiOperation(value = "干燥炉分析1接口", notes = "干燥炉分析1接口", response = MainCurveBean.class)
    @Authorization("CooL")
    @GetMapping("/days1Curve")
    public Object days1Curve(@ApiParam(value = "产线 测试数据:1" ,required=true )@RequestParam(value = "line", required = true) String line,
                            @ApiParam(value = "产线顺序" ,required=true )@RequestParam(value = "index", required = true) String index) {
        ResultMsg resultMsg;
        Date startdate = new Date();
        resultMsg = new ResultMsg(ResultStatusCode.OK.getResultCode(), ResultStatusCode.OK.getResultMessage(), daysCurveService.selectDays1(line,index));
        logger.info("干燥炉分析1时间："+(((new Date()).getTime()-startdate.getTime())/1000));
        return resultMsg;
    }

    /**
     * 干燥炉分析2
     *
     */
    @ApiOperation(value = "干燥炉分析2接口", notes = "干燥炉分析2接口", response = MainCurveBean.class)
    @Authorization("CooL")
    @GetMapping("/days2Curve")
    public Object days2Curve(@ApiParam(value = "产线 测试数据:1" ,required=true )@RequestParam(value = "line", required = true) String line,
                             @ApiParam(value = "查询开始日期 测试数据:20170709" ,required=true )@RequestParam(value = "startDate", required = true) String startDate,
                             @ApiParam(value = "查询结束日期 测试数据:20170730" ,required=true )@RequestParam(value = "endDate", required = true) String endDate,
                             @ApiParam(value = "设备 1" ,required=true )@RequestParam(value = "type", required = true) String type)
    {
        ResultMsg resultMsg;
        Date startdate = new Date();
        resultMsg = new ResultMsg(ResultStatusCode.OK.getResultCode(), ResultStatusCode.OK.getResultMessage(), daysCurveService.selectDays2(line,startDate,endDate,type));
        logger.info("干燥炉分析2时间："+(((new Date()).getTime()-startdate.getTime())/1000));
        return resultMsg;
    }

    /**
     * 烧结炉分析
     *
     */
    @ApiOperation(value = "烧结炉分析接口", notes = "烧结炉分析接口", response = SFCurveBean.class)
    @Authorization("CooL")
    @GetMapping("/sfCurve")
    public Object sfCurve(@ApiParam(value = "产线 测试数据:1" ,required=true )@RequestParam(value = "line", required = true) String line,
                             @ApiParam(value = "查询开始日期1 测试数据:20170709" ,required=true )@RequestParam(value = "startDate1", required = true) String startDate1,
                             @ApiParam(value = "查询结束日期1 测试数据:20170730" ,required=true )@RequestParam(value = "endDate1", required = true) String endDate1,
                             @ApiParam(value = "查询开始日期2 测试数据:20170709" ,required=true )@RequestParam(value = "startDate2", required = true) String startDate2,
                             @ApiParam(value = "查询结束日期2 测试数据:20170730" ,required=true )@RequestParam(value = "endDate2", required = true) String endDate2,
                          @ApiParam(value = "查询模式 1按照片 2按照小时 3按照秒" ,required=true )@RequestParam(value = "selectType", required = true) String selectType,
                             @ApiParam(value = "项目选择 1翘曲度 2烧结出炉温度 3开路电压 4短路电流 5效率 " ,required=true )@RequestParam(value = "type", required = true) String type)
    {
        ResultMsg resultMsg;
        Date startdate = new Date();

        resultMsg = new ResultMsg(ResultStatusCode.OK.getResultCode(), ResultStatusCode.OK.getResultMessage(), sfCurveService.getData(startDate1,endDate1,startDate2,endDate2,type,line,selectType));
        logger.info("烧结炉分析时间："+(((new Date()).getTime()-startdate.getTime())/1000));
        return resultMsg;
    }

    /**
     * 稼动率接口
     *
     */
    @ApiOperation(value = "稼动率接口", notes = "稼动率接口", response = CopyCurveBean.class)
    @Authorization("CooL")
    @GetMapping("/exceptionActivationCurve")
    public Object exceptionActivationCurve(@ApiParam(value = "产线 测试数据:1" ,required=true )@RequestParam(value = "line", required = true) String line,
                                  @ApiParam(value = "日期:2017-07-09" ,required=true )@RequestParam(value = "datestart", required = true) String datestart,
                                  @ApiParam(value = "日期:2018-07-09" ,required=true )@RequestParam(value = "dateend", required = true) String dateend,
                                           @ApiParam(value = "稼动率类型 测试数据:waiting" ,required=true )@RequestParam(value = "type", required = true) String type,
                                           @ApiParam(value = "设备 测试数据:2" ,required=true )@RequestParam(value = "devices", required = true) String devices) {
        ResultMsg resultMsg;
        Date startdate = new Date();
        resultMsg = new ResultMsg(ResultStatusCode.OK.getResultCode(), ResultStatusCode.OK.getResultMessage(), exceptionCurveService.selectDaysActivation(datestart,dateend,line,type,devices));
        logger.info("稼动率接口："+(((new Date()).getTime()-startdate.getTime())/1000));
        return resultMsg;
    }

    /**
     * 稼动率待机接口
     *
     */
    @ApiOperation(value = "印刷机稼动率接口", notes = "稼动率接口", response = CopyCurveBean.class)
    @Authorization("CooL")
    @GetMapping("/exceptionActivationPrintCurve")
    public Object exceptionActivationPrintCurve(@ApiParam(value = "产线 测试数据:1" ,required=true )@RequestParam(value = "line", required = true) String line,
                                           @ApiParam(value = "日期:2017-07-09" ,required=true )@RequestParam(value = "datestart", required = true) String datestart,
                                           @ApiParam(value = "日期:2018-07-09" ,required=true )@RequestParam(value = "dateend", required = true) String dateend,
                                                @ApiParam(value = "设备 测试数据:2" ,required=true )@RequestParam(value = "devices", required = true) String devices) {
        ResultMsg resultMsg;
        Date startdate = new Date();
        resultMsg = new ResultMsg(ResultStatusCode.OK.getResultCode(), ResultStatusCode.OK.getResultMessage(), exceptionCurveService.selectDaysActivationPrint(datestart,dateend,line,devices));
        logger.info("印刷机稼动率接口："+(((new Date()).getTime()-startdate.getTime())/1000));
        return resultMsg;
    }
    /**
     * 首页指标接口
     *
     */
    @ApiOperation(value = "首页指标接口", notes = "首页指标接口", response = CopyCurveBean.class)
    @Authorization("CooL")
    @GetMapping("/mainTarget")
    public Object mainTarget() {
        ResultMsg resultMsg;
        Date startdate = new Date();
        resultMsg = new ResultMsg(ResultStatusCode.OK.getResultCode(), ResultStatusCode.OK.getResultMessage(), exceptionCurveService.mainTarget());
        logger.info("首页指标接口接口："+(((new Date()).getTime()-startdate.getTime())/1000));
        return resultMsg;
    }
    /**
     * 首页设备设置顺序
     *
     */
    @ApiOperation(value = "首页设备设置顺序", notes = "首页设备设置顺序", response = CopyCurveBean.class)
    @Authorization("CooL")
    @GetMapping("/getEquipmentSort")
    public Object getEquipmentSort() {
        ResultMsg resultMsg;
        Date startdate = new Date();
        resultMsg = new ResultMsg(ResultStatusCode.OK.getResultCode(), ResultStatusCode.OK.getResultMessage(), exceptionCurveService.getEquipmentSort());
        logger.info("首页指标接口接口："+(((new Date()).getTime()-startdate.getTime())/1000));
        return resultMsg;
    }
    @GetMapping(value="testUrl")
    public String testUrl(){
        return "OK";
    }
}
